var tpl = function(dom) {
	if(typeof dom === 'object') {
		var objDom = dom;
	} else if(typeof dom === 'function') {
		var objDom = dom();
	} else if(typeof dom === 'string') {
		var objDom = document.querySelector(dom);
	} else {
		return '';
	};

	if(objDom['data-tpl']) {
		return objDom['data-tpl'];
	};

	var _if = objDom.querySelectorAll('[v-if]'),
		_for = objDom.querySelectorAll('[v-for]'),
		_html = objDom.querySelectorAll('[v-html]'),
		_text = objDom.querySelectorAll('[v-text]');

	tpl.each(_if, function(i, item) {
		var _ifVal = item.getAttribute('v-if');
		item.removeAttribute('v-if');
		var beforeT = document.createTextNode('{{# if(' + _ifVal + '){ }}');
		var afterT = document.createTextNode('{{# ' + '}' + ' }}');
		tpl.before(beforeT, item);
		tpl.after(afterT, item);
	});

	tpl.each(_for, function(i, item) {
		var _forVal = item.getAttribute('v-for');
		item.removeAttribute('v-for');
		var eachArg = tpl.vueFor(_forVal);
		var beforeT = document.createTextNode('{{# $.each(' + eachArg[0] + ',function(' + eachArg[1] + ',' + eachArg[2] + '){' + ' }}');
		var afterT = document.createTextNode('{{# ' + '})' + ' }}');
		tpl.before(beforeT, item);
		tpl.after(afterT, item);
	});

	tpl.each(_html, function(i, item) {
		var _htmlVal = item.getAttribute('v-html');
		item.removeAttribute('v-html');
		item.innerHTML = '{{ ' + _htmlVal + ' }}';
	});

	tpl.each(_text, function(i, item) {
		var _textVal = item.getAttribute('v-text');
		item.removeAttribute('v-text');
		item.innerHTML = '{{= ' + _textVal + ' }}';
	});

	objDom['data-tpl'] = objDom.innerHTML.replace(/&amp;/g, '&');

	objDom.innerHTML = '';

	return objDom['data-tpl'];
};

tpl.type = function(object) {
	return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
};

tpl.each = function(obj, fn) {
	if(tpl.type(obj) === 'object') {
		for(var i in obj) {
			fn && fn(i, obj[i])
		}
	} else {
		for(var i = 0, len = obj.length; i < len; i++) {
			fn && fn(i, obj[i])
		}
	}
};

tpl.vueFor = function(str) {
	var arg = str.split(' in ');
	var eachArg = [];
	eachArg[0] = arg[1];
	var arr1 = arg[0].replace(/^\s?\(|\)\s?$/g, '').split(',');
	if(arr1.length == 1) {
		eachArg[1] = '_i';
		eachArg[2] = arr1[0];
	} else {
		eachArg[1] = arr1[1];
		eachArg[2] = arr1[0];
	}
	return eachArg;
}

tpl.after = function(newI, exiI) {
	if(exiI.nextSibling) {
		exiI.parentNode.insertBefore(newI, exiI.nextSibling);
	} else {
		exiI.parentNode.appendChild(newI);
	};
	return exiI;
};
tpl.before = function(newI, exiI) {
	exiI.parentNode.insertBefore(newI, exiI);
	return exiI;
};

window.laytpl = function(tplDom, data, fn) {
	var _tpl = undefined;
	layui.use('laytpl', function() {
		if(_tpl == undefined) _tpl = tpl(tplDom);
		layui.laytpl(_tpl).render(data, function(html) {
			fn && fn(html)
		});
	});
	_tpl = tpl(tplDom);
};

(function() {
	var _attr = $.fn.attr,
		_html = $.fn.html,
		_append = $.fn.append,
		_prepend = $.fn.prepend,
		_replaceWith = $.fn.replaceWith;
	$.fn.extend({
		"html": function() {
			var _return = _html.apply(this, arguments);
			window.tplInit();
			return _return;
		},
		"append": function() {
			var _return = _append.apply(this, arguments);
			window.tplInit();
			return _return;
		},
		"prepend": function() {
			var _return = _prepend.apply(this, arguments);
			window.tplInit();
			return _return;
		},
		"replaceWith": function() {
			var _return = _replaceWith.apply(this, arguments);
			window.tplInit();
			return _return;
		}
	});
})();

window.tplInit = function() {
	var tplAll = document.querySelectorAll('[data-tpl]:not([data-wl])');
	$.each(tplAll, function(i, item) {
		item.setAttribute('data-wl', '');
		tpl(item);
	})
};

$(function() {
	window.tplInit();
});